<template>
	<view>
		<!-- <f-navbar title="消防技术服务（维保）" :iconLeft="true" :isBack="true" @goBack="goBack" /> -->
		<maintenance-home @getCheck="getCheck" @getTips="getTips" ref="home" v-if="tabberIndex == 0" />
		<maintenance-square @getCheck="getCheck" @getTips="getTips" @handleScroll="handleScroll"
			@handlepageScrollTo="handlepageScrollTo" ref="square" v-if="tabberIndex == 1" />
		<maintenance-project @getCheck="getCheck" @getTips="getTips" @handleScroll="handleScroll" ref="project"
			v-if="tabberIndex == 2" />
		<maintenance-personal @getCheck="getCheck" @getTips="getTips" ref="personal" v-if="tabberIndex == 3" />
		<f-tabber />
		<u-back-top :scrollTop="scrollTopList.scrollTop" top="200" mode="circle" :iconStyle="scrollTopList.iconStyle"
			:customStyle="scrollTopList.customstyle"></u-back-top>
		<u-modal v-model="checkModal" title="温馨提示" confirm-text="马上去" @confirm="checkConfirm"
			:content-style="{ 'line-height': '1.5' }" content="当前有待抽查任务需要处理, 请立即处理！" />
		<u-modal v-model="tipsState" title="工时作废警告" confirm-text="确认" @confirm="confirmTips"
			:content-style="{ 'line-height': '1.5' }" :content="tipsList" />
		<f-notice ref="notice" v-show="false" />
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import maintenanceHome from './home/maintenanceHome.vue';
	import maintenanceSquare from '../commonInside/square/Square.vue';
	import maintenanceProject from './project/maintenanceProject.vue';
	import maintenancePersonal from '../commonInside/personal/Personal.vue';
	export default {
		name: 'maintenance',
		components: {
			maintenanceHome,
			maintenanceSquare,
			maintenanceProject,
			maintenancePersonal
		},
		data() {
			return {
				// 返回顶部组件参数
				scrollTopList: {
					scrollTop: 0,
					iconStyle: {
						fontSize: '32rpx',
						color: '#FFFFFF'
					},
					customstyle: {
						background: '#3B83FE'
					}
				},
				checkModal: false, // 是否有抽查弹框显/隐
				scrollTop: null, // 距离顶部的距离
				tipsState: false, //工时警告提示弹窗显/隐
				tipsList: '', // 工时警告提示弹窗内容
				tipsId: '', // 工时作废id
			};
		},
		// 页面滚动距离顶部的数据
		onPageScroll(e) {
			this.scrollTopList.scrollTop = e.scrollTop;
			this.scrollTop = e.scrollTop // 记录当前的位置
		},
		computed: {
			// 导航栏当前点击的index
			...mapState({
				tabberIndex: modules => modules.tabber.tabberIndex
			})
		},
		// 页面关闭清空滚动距离
		onUnload() {
			uni.setStorageSync('scrollVal', 0)
		},
		onLoad() {},
		onShow() {
			// uni.hideHomeButton(); // 隐藏小房子
			this.$refs.notice.getNotice() // 获取公告列表
			// 动态刷新数据
			this.$nextTick(() => {
				if (this.tabberIndex == 0) {
					this.$refs.home.showLoading();
					return;
				}
				if (this.tabberIndex == 1) {
					this.$refs.square.showLoading();
					// 位置清 0
					uni.pageScrollTo({
						scrollTop: 0,
						duration: 0
					})
					this.$nextTick(() => {
						uni.pageScrollTo({
							scrollTop: uni.getStorageSync('scrollVal'),
							duration: 1000
						});
					})
					return;
				}
				if (this.tabberIndex == 2) {
					this.$refs.project.showLoading();
					// 位置清 0
					uni.pageScrollTo({
						scrollTop: 0,
						duration: 0
					})
					this.$nextTick(() => {
						uni.pageScrollTo({
							scrollTop: uni.getStorageSync('scrollVal'),
							duration: 1000
						});
					})
					return;
				}
				if (this.tabberIndex == 3) {
					this.$refs.personal.showLoading();
					return;
				}
			});
		},
		// 上拉加载
		onReachBottom() {
			if (this.tabberIndex == 0) {
				this.$refs.home.pullLoading();
				return;
			}
			if (this.tabberIndex == 1) {
				this.$refs.square.pullLoading();
				return;
			}
			if (this.tabberIndex == 2) {
				this.$refs.project.pullLoading();
				return;
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.getSpotCheck() // 抽查列表
			this.getQueryUserNotReadLog() // 工时提示警告检测
			this.$refs.notice.getNotice() // 获取公告列表
			if (this.tabberIndex == 0) {
				this.$refs.home.downLoading();
				return;
			}
			if (this.tabberIndex == 1) {
				this.$refs.square.downLoading();
				return;
			}
			if (this.tabberIndex == 2) {
				this.$refs.project.downLoading();
				return;
			}
			if (this.tabberIndex == 3) {
				this.$refs.personal.downLoading();
				return;
			}
		},
		methods: {
			...mapMutations(['changeTabberIndex']),
			// 返回首页
			goBack() {
				this.changeTabberIndex(0); // 底部导航栏Index默认为0
				this.$pageJump.reLaunch('/pages/homeCentral/Central');
			},
			// 子组件传过来需要抽查的任务
			getCheck() {
				this.checkModal = true;
			},
			// 抽查--确认操作
			checkConfirm() {
				this.$pageJump.navigateTo('/pages/maintenance/public/personal/spotTask/spotTask');
			},
			// 判断是否需要抽查
			async getSpotCheck() {
				const data = await this.$http.post({
					url: '/serviceOrg/checkTask/userPage',
					data: {
						current: 1,
						size: 1,
						checkStatus: 1
					}
				});
				// console.log('有待抽查任务需要处理,请立即处理-----', data);
				if (data.code == 200 && data.data.records.length > 0) {
					let receiveTime = data.data.records[0].receiveTime // 系统开始抽查时间
					if (this.diffTime(receiveTime) < 15) {
						this.getCheck() // 打开抽查列表
					}
				}
			},
			// 计算时间差
			diffTime(start) {
				let staytimeGap = new Date().getTime() - new Date(start).getTime();
				let stayMin = Math.floor(staytimeGap / (60 * 1000)); // 分钟
				return stayMin
			},
			// 子组件过来的作废提醒
			getTips(dat) {
				this.tipsList =
					`${dat.cancelDepartmentName}在${dat.cancelTime}作废了项目【${dat.projectName}】的有效工时，请重新开始执业。如有异议，请联系对应的救援机构。`
				this.tipsId = dat.id
				this.tipsState = true
			},
			// 判断是否有未读作废提醒
			async getQueryUserNotReadLog() {
				const data = await this.$http.post({
					url: '/serviceOrg/queryUserNotReadLog'
				});
				// console.log('判断是否有未读作废提醒-----', data);
				if (data.code == 200 && data.data.cancelDepartmentName) {
					let dat = data.data
					this.tipsState = true
					this.tipsList =
						`${dat.cancelDepartmentName}在${dat.cancelTime}作废了项目【${dat.projectName}】的有效工时，请重新开始执业。如有异议，请联系对应的救援机构。`
					this.tipsId = dat.id
				}
			},
			// 确定并阅读了工时作废警告
			async confirmTips() {
				const data = await this.$http.post({
					url: `/serviceOrg/userReadLog/${this.tipsId}`
				})
				// console.log('确定并阅读了工时作废警告', data)
				if (data.code == 200) {} else {
					this.$refs.uToast.show({
						title: data.msg,
						type: 'error'
					});
				}
			},
			// 项目列表点击进入详情---返回的时候记录的项目的位置
			handleScroll() {
				uni.setStorageSync('scrollVal', this.scrollTop)
			},
			// 广场切换类型的时候 ---回到顶部
			handlepageScrollTo() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 1000
				});
			}
		},
		onShareAppMessage() {
			return {
				title: '分享小程序给朋友',
				path: '/pages/homePage/Home'
			};
		}
	};
</script>

<style lang="scss" scoped>
	/deep/.maintenanceHome-top-tipsBox-left .u-badge--bg--error {
		right: 34% !important;
	}

	/deep/.u-btn--default {
		color: #bcbfcc !important;
		border-color: #bcbfcc !important;
	}

	/deep/.u-size-mini {
		height: 74rpx !important;
		line-height: 74rpx !important;
		padding: 0 47rpx !important;
		margin-left: 40rpx !important;
		font-size: 28rpx !important;
	}
</style>